package com.sinothk.fc.storeOrderDetail.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.storeOrderDetail.entity.StoreOrderDetailEntity;
import com.sinothk.fc.storeOrderDetail.entity.StoreOrderDetailSearchReq;
import com.sinothk.fc.storeOrderDetail.entity.StoreOrderDetailVO;
import com.sinothk.fc.storeOrderDetail.service.IStoreOrderDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 门店销售记录表
 * @Author: Liangyt
 * @Date: 2022-09-14
 * @Version: V1.0
 */
@Api(tags = "门店销售记录表")
@RestController
@RequestMapping("/fc/storeOrderRecordCtrl")
@Slf4j
public class StoreOrderDetailController extends JeecgController<StoreOrderDetailEntity, IStoreOrderDetailService> {

    private final IStoreOrderDetailService storeOrderRecordService;

    public StoreOrderDetailController(IStoreOrderDetailService storeOrderRecordService) {
        this.storeOrderRecordService = storeOrderRecordService;
    }

    /**
     * 分页列表查询
     *
     * @param storeSaleRecordEntity
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "门店销售记录表-分页列表查询")
    @ApiOperation(value = "门店销售记录表-分页列表查询", notes = "门店销售记录表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StoreOrderDetailEntity storeSaleRecordEntity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<StoreOrderDetailEntity> queryWrapper = QueryGenerator.initQueryWrapper(storeSaleRecordEntity, req.getParameterMap());
        Page<StoreOrderDetailEntity> page = new Page<StoreOrderDetailEntity>(pageNo, pageSize);
        IPage<StoreOrderDetailEntity> pageList = storeOrderRecordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "门店销售记录表-分页列表查询")
    @ApiOperation(value = "门店销售记录表-分页列表查询", notes = "门店销售记录表-分页列表查询")
    @GetMapping(value = "/listForStore")
    public Result<?> listForStore(StoreOrderDetailSearchReq searchReq,
                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<StoreOrderDetailEntity> pageList = storeOrderRecordService.listForStore(new Page<StoreOrderDetailEntity>(pageNo, pageSize), searchReq);
        return Result.OK(pageList);
    }

    /**
     * 订单列表-订单分组并统计一天营业额
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "订单列表-订单分组并统计一天营业额")
    @ApiOperation(value = "订单列表-订单分组并统计一天营业额", notes = "订单列表-订单分组并统计一天营业额")
    @GetMapping(value = "/listForStoreGroupByOrderId")
    public Result<?> listForStoreGroupByOrderId(StoreOrderDetailSearchReq searchReq,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<StoreOrderDetailVO> pageList = storeOrderRecordService.listForStoreGroupByOrderId(new Page<>(pageNo, pageSize), searchReq);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param storeSaleRecordEntity
     * @return
     */
    @AutoLog(value = "门店销售记录表-添加")
    @ApiOperation(value = "门店销售记录表-添加", notes = "门店销售记录表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StoreOrderDetailEntity storeSaleRecordEntity) {
        storeOrderRecordService.save(storeSaleRecordEntity);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param storeSaleRecordEntity
     * @return
     */
    @AutoLog(value = "门店销售记录表-编辑")
    @ApiOperation(value = "门店销售记录表-编辑", notes = "门店销售记录表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StoreOrderDetailEntity storeSaleRecordEntity) {
        storeOrderRecordService.updateById(storeSaleRecordEntity);
        return Result.OK("编辑成功!");
    }

//    /**
//     * 通过id删除
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "门店销售记录表-通过id删除")
//    @ApiOperation(value = "门店销售记录表-通过id删除", notes = "门店销售记录表-通过id删除")
//    @DeleteMapping(value = "/delete")
//    public Result<?> delete(@RequestParam(name = "id") String id) throws Exception {
//        storeOrderRecordService.delete(id);
//        return Result.OK("删除成功!");
//    }

//    /**
//     * 批量删除
//     *
//     * @param ids
//     * @return
//     */
//    @AutoLog(value = "门店销售记录表-批量删除")
//    @ApiOperation(value = "门店销售记录表-批量删除", notes = "门店销售记录表-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
//    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//        this.storeOrderRecordService.removeByIds(Arrays.asList(ids.split(",")));
//        return Result.OK("批量删除成功!");
//    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "门店销售记录表-通过id查询")
    @ApiOperation(value = "门店销售记录表-通过id查询", notes = "门店销售记录表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        StoreOrderDetailEntity storeSaleRecordEntity = storeOrderRecordService.getById(id);
        if (storeSaleRecordEntity == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(storeSaleRecordEntity);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param storeSaleRecordEntity
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StoreOrderDetailEntity storeSaleRecordEntity) {
        return super.exportXls(request, storeSaleRecordEntity, StoreOrderDetailEntity.class, "门店销售记录表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StoreOrderDetailEntity.class);
    }

}
